package com.clayecloud.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfig {

    //前台api扫描地址
    @Value("${knife4j.BASE_PATH_FRONT}")
    String BASE_PATH_FRONT ="com.fireboom.common.client.Controller";

    //后台api扫描地址
    @Value("${knife4j.BASE_PATH_BACK}")
    String BASE_PATH_BACK ="com.fireboom.common.admin.Controller";

    //首页标题
    @Value("${knife4j.TITLE}")
    String TITLE = "swagger-bootstrap-ui RESTful APIs";

    //首页项目描述
    @Value("${knife4j.DESCRIPTION}")
    String DESCRIPTION = "swagger-bootstrap-ui";

    //项目运行地址
    @Value("${knife4j.SERVICE_URL}")
    String SERVICE_URL = "http://localhost:8080/";

    //项目开发者联系信息
    @Value("${knife4j.CONTACT}")
    String CONTACT = "developer@mail.com";

    //项目版本号
    @Value("${knife4j.VERSION}")
    String VERSION = "1.0";


    /**
     * 前台API分组
     *
     * @return
     */
    @Bean(value = "indexApi")
    public Docket indexApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("客户端API分组")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PATH_FRONT))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 后台API分组
     *
     * @return
     */
    @Bean(value = "adminApi")
    public Docket adminApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理端API分组")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PATH_BACK))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 设置项目信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        ApiInfo build = new ApiInfoBuilder()
                .title(TITLE)
                .description(DESCRIPTION)
                .termsOfServiceUrl(SERVICE_URL)
                .contact(new Contact(CONTACT,"",""))
                .version(VERSION)
                .build();
        return build;
    }


}
